package com.hzsparrow.ai.config;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.FileCopyUtils;

import jakarta.annotation.PostConstruct;

/**
 * 提示词配置类
 */
@Configuration
@ConfigurationProperties(prefix = "ai.prompt")
public class PromptConfiguration {

    private String generalChatPrompt = "你是一个友好的AI助手，请用中文回答用户的问题。回答要专业、准确、有帮助性。";
    private String vectorChatPrompt = "";
    
    // 提示词文件路径配置
    private String generalChatPromptFile = "prompts/general-chat-prompt.txt";
    private String vectorChatPromptFile = "prompts/vector-chat-prompt.txt";

    @PostConstruct
    public void init() {
        try {
            // 从文件加载通用聊天提示词
            if (generalChatPromptFile != null && !generalChatPromptFile.isEmpty()) {
                generalChatPrompt = readPromptFromFile(generalChatPromptFile);
            }
            
            // 从文件加载向量聊天提示词
            if (vectorChatPromptFile != null && !vectorChatPromptFile.isEmpty()) {
                vectorChatPrompt = readPromptFromFile(vectorChatPromptFile);
            }
        } catch (IOException e) {
            System.err.println("加载提示词文件时出错: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    /**
     * 从类路径资源文件中读取提示词
     * 
     * @param filePath 提示词文件路径
     * @return 提示词内容
     * @throws IOException 如果读取失败
     */
    private String readPromptFromFile(String filePath) throws IOException {
        Resource resource = new ClassPathResource(filePath);
        byte[] bytes = FileCopyUtils.copyToByteArray(resource.getInputStream());
        return new String(bytes, StandardCharsets.UTF_8);
    }

    public String getGeneralChatPrompt() {
        return generalChatPrompt;
    }

    public void setGeneralChatPrompt(String generalChatPrompt) {
        this.generalChatPrompt = generalChatPrompt;
    }

    public String getVectorChatPrompt() {
        return vectorChatPrompt;
    }

    public void setVectorChatPrompt(String vectorChatPrompt) {
        this.vectorChatPrompt = vectorChatPrompt;
    }
    
    public String getGeneralChatPromptFile() {
        return generalChatPromptFile;
    }

    public void setGeneralChatPromptFile(String generalChatPromptFile) {
        this.generalChatPromptFile = generalChatPromptFile;
    }

    public String getVectorChatPromptFile() {
        return vectorChatPromptFile;
    }

    public void setVectorChatPromptFile(String vectorChatPromptFile) {
        this.vectorChatPromptFile = vectorChatPromptFile;
    }
} 